<template>
  <view class="jnpf-wrap jnpf-wrap-workflow">
    <u-form
      :model="dataForm"
      ref="dataForm"
      :errorType="['toast']"
      label-position="left"
      label-width="150"
      label-align="left"
    >
      <u-form-item label="变更单别" v-if="judgeShow('ta001')">
        <u-input
          v-model="dataForm.ta001"
          placeholder="变更单别"
          :disabled="judgeWrite('ta001')"
        ></u-input>
      </u-form-item>
      <u-form-item label="变更单号" v-if="judgeShow('ta002')">
        <u-input
          v-model="dataForm.ta002"
          placeholder="变更单号"
          :disabled="judgeWrite('ta002')"
        ></u-input>
      </u-form-item>
      <u-form-item label="变更日期" v-if="judgeShow('ta003')">
        <u-input
          v-model="dataForm.ta003"
          placeholder="变更日期"
          :disabled="judgeWrite('ta003')"
        ></u-input>
      </u-form-item>
      <view class="jnpf-table" style="margin-top: 24rpx">
        <view
          class="jnpf-table-item"
          style="margin-bottom: 24rpx"
          v-for="(item, i) in dataForm.bomtbList"
          :key="i"
        >
          <u-form-item label="序号" style="background-color: aqua">
            {{ i + 1 }}
          </u-form-item>
          <u-form-item label="品名" v-if="judgeShow('mb002')">
            <u-input v-model="item.mb002" placeholder="品名" disabled></u-input>
          </u-form-item>
          <u-form-item label="规格" v-if="judgeShow('mb003')">
            <u-input
              v-model="item.mb003"
              placeholder="请输入规格"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="子件关系" v-if="judgeShow('tb019')">
            <u-tag :text="getTb019Computed(item.tb019)" type="success" />
          </u-form-item>
          <u-form-item label="备注" v-if="judgeShow('tb122')">
            <u-input
              v-model="item.tb122"
              type="textarea"
              placeholder="请输入备注"
              disabled
            ></u-input>
          </u-form-item>
        </view>
      </view>
    </u-form>
    <u-modal v-model="show">
      <scroll-view
        :scroll-top="scrollTop"
        scroll-y="true"
        style="height: 700rpx"
      >
        <u-form :model="showForm" v-for="(item, i) in showForm">
          <u-form-item label="序号" style="background-color: aqua">
            {{ i + 1 }}
          </u-form-item>
          <u-form-item label="元件品号">
            <u-input v-model="item.cb005" disabled />
          </u-form-item>
          <u-form-item label="品名">
            <u-input v-model="item.mb002" disabled />
          </u-form-item>
          <u-form-item label="规格">
            <u-input v-model="item.mb003" disabled />
          </u-form-item>
          <u-form-item label="单位">
            <u-input v-model="item.cb006" disabled />
          </u-form-item>
          <u-form-item label="属性">
            {{ item.mb025 + ":" + item.fullName }}
          </u-form-item>
        </u-form>
      </scroll-view>
    </u-modal>
  </view>
</template>

<script>
import comMixin from "../mixin";
import {
  getGoodsList,
  getCustomerList,
  Info,
  Create,
  Update,
} from "@/api/apply/order";
import { getBomta, BomcbListInit } from "@/api/workFlow/flowBefore";
import {
  getDictionaryDataSelector,
  getDataInterfaceDataInfo,
} from "@/api/common";
export default {
  mixins: [comMixin],
  computed: {
    getTb019Computed() {
      return (id) => {
        const options = this.options.filter((item) => id.includes(item.id));
        return options.length > 0 ? options[0].fullName : "";
      };
    },
  },
  data() {
    return {
      show: false,
      dataForm: {},
      id: "",
      showForm: [],
      formOperatesList: [],
      tb019Options: [],
      options: [
        {
          fullName: "必选，唯一",
          id: "1",
        },
        {
          fullName: "必选，至少一个",
          id: "2",
        },
        {
          fullName: "可选，唯一",
          id: "3",
        },
        {
          fullName: "任意选",
          id: "4",
        },
      ],
    };
  },
  methods: {
    init(data, id, flowFormContentJson) {
      this.setting = data;
      this.formOperatesList = data.formOperates;
      this.id = id;
      this.getInfoFn(id);
    },
    getInfoFn(id) {
      let list = id.split("-");
      let ids = {
        ta001: list[1],
        ta002: list[2],
      };
      getBomta(ids).then((res) => {
        if (res.code == 200) {
          this.dataForm = res.data;
          this.tb019Options = this.options.filter((item) =>
            this.dataForm.tb019.includes(item.id)
          );
        }
      });
    },
    queryChildBachelor(tb004) {
      let query = {
        tb004,
      };
      BomcbListInit(query).then((res) => {
        this.showForm = res.data;
        this.show = true;
      });
    },
    getDataInterfaceDataInfoMA003(id) {
      let data = {
        id,
        interfaceId: "362399109623061061",
        propsValue: "MB001",
        relationField: "MB001",
      };
      getDataInterfaceDataInfo("362399109623061061", data).then((res) => {});
    },
    selfGetInfo() {
      Info(this.setting.id).then((res) => {
        this.dataForm = res.data;
        if (this.dataForm.fileJson) {
          this.fileList = JSON.parse(this.dataForm.fileJson);
        }
      });
    },
    selfSubmit() {
      this.dataForm.status = this.eventType === "submit" ? 0 : 1;
      if (this.eventType === "save") return this.selfHandleRequest();
      uni.showModal({
        title: "提示",
        content: "您确定要提交当前流程吗？",
        success: (res) => {
          if (res.confirm) {
            this.selfHandleRequest();
          }
        },
      });
    },
    selfHandleRequest() {
      if (!this.dataForm.id) delete this.dataForm.id;
      const formMethod = this.dataForm.id ? Update : Create;
      formMethod(this.dataForm).then((res) => {
        uni.showToast({
          title: res.msg,
          complete: () => {
            setTimeout(() => {
              uni.$emit("refresh");
              uni.navigateBack();
            }, 1500);
          },
        });
      });
    },
    initData() {
      getGoodsList().then((res) => {
        this.goodsOptions = res.data.list;
      });
      getCustomerList().then((res) => {
        this.customerOptions = res.data.list;
      });
    },
    selfInit() {
      this.addGoods();
      this.addPlan();
    },
    onChange(e) {
      const user = e[0];
      this.dataForm.salesmanName = user.fullName;
    },
    addGoods() {
      const item = {
        goodsId: "",
        goodsCode: "",
        goodsName: "",
        specifications: "",
        unit: "",
        qty: "",
        price: "",
        amount: "",
        discount: null,
        cess: null,
        actualPrice: "",
        actualAmount: "",
        description: "",
      };
      this.dataForm.goodsList.push(item);
    },
    delGoods(index) {
      this.dataForm.goodsList.splice(index, 1);
    },
    addPlan() {
      const item = {
        receivableDate: "",
        receivableRate: "",
        receivableMoney: "",
        receivableMode: "",
        abstract: "",
      };
      this.dataForm.collectionPlanList.push(item);
    },
    delPlan(index) {
      this.dataForm.collectionPlanList.splice(index, 1);
    },
    onGoodsChange(e, i) {
      const goods = e[0];
      this.dataForm.goodsList[i] = {
        goodsId: goods.id,
        goodsCode: goods.code,
        goodsName: goods.text,
        specifications: goods.specifications,
        unit: goods.unit,
        qty: 1,
        price: goods.price,
        amount: goods.price,
        discount: 100,
        cess: 0,
        actualPrice: goods.price,
        actualAmount: goods.price,
        description: "",
      };
      this.$forceUpdate();
    },
    onCustomerChange(e) {
      const customer = e[0];
      this.dataForm.customerName = customer.text;
    },
    count(row) {
      //金额 = 数量*单价
      row.amount = this.jnpf.toDecimal(
        parseFloat(row.price) * parseFloat(row.qty)
      );
      //折扣价 = (单价*折扣)
      var discountPrice = row.price * (row.discount / 100);
      //实际单价 = 折扣价 * (1 + (税率 / 100))
      row.actualPrice = this.jnpf.toDecimal(
        discountPrice * (1 + row.cess / 100)
      );
      //实际金额
      row.actualAmount = this.jnpf.toDecimal(
        parseFloat(row.actualPrice) * parseFloat(row.qty)
      );
      this.$forceUpdate();
    },
  },
};
</script>
